home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre1.z / postgre1 / sample / nfstest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-27  |  11.9 KB  |  651 lines

  1. /*    
  2.  *  nfstest -- benchmark Inversion over the network.
  3.  *
  4.  *    This test performs the following operations:
  5.  *        Create a 25MByte file (1MByte writes)
  6.  *        Read 1MByte sequentially
  7.  *          + in a single transfer
  8.  *          + in transfers of page-sized units
  9.  *        Overwrite 1MByte sequentially
  10.  *          + in a single transfer
  11.  *          + in transfers of page-sized units
  12.  *        Read 1MByte in page-sized units, randomly distributed
  13.  *          throughout the file, aligned at page boundaries
  14.  *        Overwrite 1MByte in page-sized units, randomly distributed
  15.  *          throughout the file, aligned at page boundaries
  16.  *        Read 1 byte from a random location in the file
  17.  *        Overwrite 1 byte at a random location in the file
  18.  */
  19.  
  20. #include <stdio.h>
  21. #include <sys/types.h>
  22. #include <sys/file.h>
  23. #include <sys/errno.h>
  24. #include <sys/time.h>
  25. #include <sys/resource.h>
  26.  
  27. #ifndef sprite
  28. #include <sys/signal.h>
  29. #endif /* !sprite */
  30.  
  31. #include "tmp/c.h"
  32. #include "tmp/oid.h"
  33. #include "tmp/libpq-fe.h"
  34. #include "tmp/libpq-fs.h"
  35.  
  36. #define Inversion 0
  37. #define BIG    1
  38. #define    SMALL    0
  39.  
  40. RcsId("$Header: /private/postgres/sample/RCS/nfstest.c,v 1.1 1992/04/20 05:04:11 mao Exp $");
  41.  
  42. extern char    *getenv();
  43. extern char    *get_attr();
  44.  
  45. extern char     *PQhost;     /* machine on which the backend is running */
  46. extern char     *PQport;     /* comm port with the postgres backend */
  47.  
  48. #define DEFHOST        "toe.CS.Berkeley.EDU"
  49. #define FILEDB        "mao"
  50. #define FILENAME    "mao_nfs"
  51.  
  52. char QryBuf[512];
  53.  
  54. main(argc,argv)
  55.      int argc;
  56.      char **argv;
  57. {
  58.     int fd;
  59.     int stat;
  60.  
  61.     if (argc == 1)
  62.     PQhost = DEFHOST;
  63.     else if (argc == 2)
  64.     PQhost = argv[1];
  65.     else {
  66.     fprintf(stderr, "usage: %s [host]\n", *argv);
  67.     exit (1);
  68.     }
  69.  
  70.     srandom(time(0L));
  71.  
  72.     PQsetdb(FILEDB);
  73.  
  74.     printf("rem 0 setup|"); fflush(stdout);
  75.     stat = setup();
  76.  
  77.     if (!stat) {
  78.     flushcache();
  79.     printf("rem 1 big read|"); fflush(stdout);
  80.     stat = readtest(BIG);
  81.     }
  82.  
  83.     if (!stat) {
  84.     flushcache();
  85.     printf("rem 2 small read|"); fflush(stdout);
  86.     stat = readtest(SMALL);
  87.     }
  88.  
  89.     if (!stat) {
  90.     flushcache();
  91.     printf("rem 3 random read|"); fflush(stdout);
  92.     stat = randread();
  93.     }
  94.  
  95.     if (!stat) {
  96.     flushcache();
  97.     printf("rem 4 single-byte read|"); fflush(stdout);
  98.     stat = readone();
  99.     }
  100.  
  101.     if (!stat) {
  102.     flushcache();
  103.     printf("rem 5 big write|"); fflush(stdout);
  104.     stat = writetest(BIG);
  105.     }
  106.  
  107.     if (!stat) {
  108.     flushcache();
  109.     printf("rem 6 small write|"); fflush(stdout);
  110.     stat = writetest(SMALL);
  111.     }
  112.  
  113.     if (!stat) {
  114.     flushcache();
  115.     printf("rem 7 random write|"); fflush(stdout);
  116.     stat = randwrite();
  117.     }
  118.  
  119.     if (!stat) {
  120.     flushcache();
  121.     printf("rem 8 single-byte write"); fflush(stdout);
  122.     stat = writeone();
  123.     }
  124.  
  125.     PQfinish();
  126.     exit (stat);
  127. }
  128.  
  129. flushcache()
  130. {
  131.     char *res;
  132.  
  133.     sprintf(QryBuf, "retrieve (x = userfntest(200))");
  134.  
  135.     res = PQexec(QryBuf);
  136.  
  137.     if (*res == 'E') {
  138.     fprintf(stderr, "%s\nflushcache failed\n", ++res);
  139.     return;
  140.     }
  141. }
  142.  
  143. /* MByte == 2**20; MYMBYTE == 8092 * 129 (as close as i can get) */
  144. #define    MEGABYTE    1048576
  145. #define MYMEGABYTE    1043868
  146. #define TWENTYFIVE    10
  147.  
  148. int
  149. setup(fd)
  150.     int fd;
  151. {
  152.     int nbytes;
  153.     int want, nwrite;
  154.     char *res;
  155.     char *buf;
  156.     int *varlen;
  157.  
  158.     if ((buf = (char *) palloc(MYMEGABYTE + 4)) == (char *) NULL) {
  159.     fprintf(stderr, "cannot palloc buf\n");
  160.     return (1);
  161.     }
  162.  
  163.     varlen = (int *) buf;
  164.  
  165.     myResetUsage();
  166.     sprintf(QryBuf, "begin");
  167.     res = PQexec(QryBuf);
  168.  
  169.     if (*res == 'E') {
  170.     fprintf(stderr, "cannot begin xact\n");
  171.     return (1);
  172.     }
  173.  
  174.     fd = p_creat(FILENAME, INV_WRITE, Inversion);
  175.  
  176.     want = TWENTYFIVE * MEGABYTE;
  177.     while (want > 0) {
  178.     if (want < MYMEGABYTE)
  179.         *varlen = want + sizeof(int);
  180.     else
  181.         *varlen = MYMEGABYTE + sizeof(int);
  182.     if ((nbytes = p_write(fd, buf, *varlen)) < *varlen - 4) {
  183.         fprintf(stderr, "setup: write failed\n");
  184.         fflush(stderr);
  185.         return (1);
  186.     }
  187.     want -= nbytes;
  188.     }
  189.  
  190.     pfree (buf);
  191.  
  192.     sprintf(QryBuf, "end");
  193.     res = PQexec(QryBuf);
  194.  
  195.     if (*res == 'E') {
  196.     fprintf(stderr, "cannot end xact\n");
  197.     return (1);
  198.     }
  199.     myShowUsage();
  200.  
  201.     return (0);
  202. }
  203.  
  204. int
  205. writetest(big)
  206.     int big;
  207. {
  208.     int fd;
  209.     int i, j;
  210.     int nbytes;
  211.     int nwritten;
  212.     int size;
  213.     char *res;
  214.     char *buf;
  215.     int *varlen;
  216.  
  217.     if (big) {
  218.     size = MEGABYTE;
  219.     } else {
  220.     size = 8092;
  221.     }
  222.  
  223.     if ((buf = (char *) palloc(size + 4)) == (char *) NULL) {
  224.     fprintf(stderr, "cannot palloc buf\n");
  225.     return (1);
  226.     }
  227.     varlen = (int *) buf;
  228.  
  229.     sprintf(QryBuf, "begin");
  230.     res = PQexec(QryBuf);
  231.  
  232.     if (*res == 'E') {
  233.     fprintf(stderr, "cannot begin xact\n");
  234.     return (1);
  235.     }
  236.  
  237.     if ((fd = p_open(FILENAME, INV_WRITE)) < 0) {
  238.     fprintf(stderr, "cannot open file\n");
  239.     return (1);
  240.     }
  241.  
  242.     if (p_lseek(fd, 0L, L_SET) < 0) {
  243.     fprintf(stderr, "cannot lseek\n");
  244.     return (1);
  245.     }
  246.  
  247.     myResetUsage();
  248.  
  249.     nwritten = 0;
  250.  
  251.     while (nwritten < MEGABYTE) {
  252.     if (size > MEGABYTE - nwritten)
  253.          size = MEGABYTE - nwritten;
  254.     *varlen = size + 4;
  255.     if ((nbytes = p_write(fd, buf, *varlen)) != *varlen - 4) {
  256.         fprintf(stderr, "expected %d, got %d\n", size, nbytes);
  257.         fflush(stderr);
  258.         return (1);
  259.     }
  260.     nwritten += nbytes;
  261.     }
  262.  
  263.     myShowUsage();
  264.  
  265.     sprintf(QryBuf, "end");
  266.     res = PQexec(QryBuf);
  267.  
  268.     if (*res == 'E') {
  269.     fprintf(stderr, "cannot end xact\n");
  270.     return (1);
  271.     }
  272.  
  273.     pfree(buf);
  274.     return (0);
  275. }
  276. int
  277. readtest(big)
  278.     int big;
  279. {
  280.     int fd;
  281.     int i, j;
  282.     int nbytes;
  283.     int nread;
  284.     int size;
  285.     char *res;
  286.     char *buf;
  287.     int *varlen;
  288.  
  289.     if (big) {
  290.     size = MEGABYTE;
  291.     } else {
  292.     size = 8092;
  293.     }
  294.  
  295.     if ((buf = (char *) palloc(size + 4)) == (char *) NULL) {
  296.     fprintf(stderr, "cannot palloc buf\n");
  297.     return (1);
  298.     }
  299.  
  300.     sprintf(QryBuf, "begin");
  301.     res = PQexec(QryBuf);
  302.  
  303.     if (*res == 'E') {
  304.     fprintf(stderr, "cannot begin xact\n");
  305.     return (1);
  306.     }
  307.  
  308.     if ((fd = p_open(FILENAME, INV_READ)) < 0) {
  309.     fprintf(stderr, "cannot open file\n");
  310.     return (1);
  311.     }
  312.  
  313.     if (p_lseek(fd, 0L, L_SET) < 0) {
  314.     fprintf(stderr, "cannot lseek\n");
  315.     return (1);
  316.     }
  317.  
  318.     myResetUsage();
  319.  
  320.     nread = 0;
  321.  
  322.     while (nread < MEGABYTE) {
  323.     if (size > MEGABYTE - nread)
  324.         size = MEGABYTE - nread;
  325.     if ((nbytes = p_read(fd, buf, size)) != size) {
  326.         fprintf(stderr, "expected %d, got %d\n", size, nbytes);
  327.         fflush(stderr);
  328.         return (1);
  329.     }
  330.     nread += nbytes;
  331.     }
  332.  
  333.     myShowUsage();
  334.  
  335.     sprintf(QryBuf, "end");
  336.     res = PQexec(QryBuf);
  337.  
  338.     if (*res == 'E') {
  339.     fprintf(stderr, "cannot end xact\n");
  340.     return (1);
  341.     }
  342.  
  343.     pfree(buf);
  344.     return (0);
  345. }
  346.  
  347. int
  348. randread()
  349. {
  350.     int fd;
  351.     int i, j;
  352.     int nbytes;
  353.     int nread;
  354.     char *res;
  355.     char *buf;
  356.     int *varlen;
  357.     int size;
  358.     int off;
  359.  
  360.     size = 8092;
  361.     if ((buf = (char *) palloc(size + 4)) == (char *) NULL) {
  362.     fprintf(stderr, "cannot palloc buf\n");
  363.     return (1);
  364.     }
  365.  
  366.     sprintf(QryBuf, "begin");
  367.     res = PQexec(QryBuf);
  368.  
  369.     if (*res == 'E') {
  370.     fprintf(stderr, "cannot begin xact\n");
  371.     return (1);
  372.     }
  373.  
  374.     if ((fd = p_open(FILENAME, INV_READ)) < 0) {
  375.     fprintf(stderr, "cannot open file\n");
  376.     return (1);
  377.     }
  378.  
  379.     myResetUsage();
  380.  
  381.     nread = 0;
  382.  
  383.     while (nread < MEGABYTE) {
  384.  
  385.     off = ((random() % ((TWENTYFIVE - 1) * MEGABYTE)) / 8092) * 8092;
  386.     if (p_lseek(fd, off, L_SET) < 0) {
  387.         fprintf(stderr, "cannot lseek\n");
  388.         return (1);
  389.     }
  390.  
  391.     if (size > MEGABYTE - nread)
  392.         size = MEGABYTE - nread;
  393.     if ((nbytes = p_read(fd, buf, size)) != size) {
  394.         fprintf(stderr, "expected %d, got %d\n", size, nbytes);
  395.         fflush(stderr);
  396.         return (1);
  397.     }
  398.     nread += nbytes;
  399.     }
  400.  
  401.     myShowUsage();
  402.  
  403.     sprintf(QryBuf, "end");
  404.     res = PQexec(QryBuf);
  405.  
  406.     if (*res == 'E') {
  407.     fprintf(stderr, "cannot end xact\n");
  408.     return (1);
  409.     }
  410.  
  411.     pfree(buf);
  412.     return (0);
  413. }
  414.  
  415. int
  416. randwrite()
  417. {
  418.     int fd;
  419.     int i, j;
  420.     int nbytes;
  421.     int nwritten;
  422.     char *res;
  423.     char *buf;
  424.     int *varlen;
  425.     int size;
  426.     int off;
  427.  
  428.     size = 8092;
  429.     if ((buf = (char *) palloc(size + 4)) == (char *) NULL) {
  430.     fprintf(stderr, "cannot palloc buf\n");
  431.     return (1);
  432.     }
  433.  
  434.     sprintf(QryBuf, "begin");
  435.     res = PQexec(QryBuf);
  436.  
  437.     if (*res == 'E') {
  438.     fprintf(stderr, "cannot begin xact\n");
  439.     return (1);
  440.     }
  441.  
  442.     if ((fd = p_open(FILENAME, INV_READ)) < 0) {
  443.     fprintf(stderr, "cannot open file\n");
  444.     return (1);
  445.     }
  446.  
  447.     myResetUsage();
  448.  
  449.     nwritten = 0;
  450.  
  451.     while (nwritten < MEGABYTE) {
  452.  
  453.     off = ((random() % ((TWENTYFIVE - 1) * MEGABYTE)) / 8092) * 8092;
  454.     if (p_lseek(fd, off, L_SET) < 0) {
  455.         fprintf(stderr, "cannot lseek\n");
  456.         return (1);
  457.     }
  458.  
  459.     if (size > MEGABYTE - nwritten)
  460.         size = MEGABYTE - nwritten;
  461.     *varlen = size + sizeof(int);
  462.     if ((nbytes = p_write(fd, buf, *varlen)) != *varlen - 4) {
  463.         fprintf(stderr, "expected %d, got %d\n", size, nbytes);
  464.         fflush(stderr);
  465.         return (1);
  466.     }
  467.     nwritten += nbytes;
  468.     }
  469.  
  470.     myShowUsage();
  471.  
  472.     sprintf(QryBuf, "end");
  473.     res = PQexec(QryBuf);
  474.  
  475.     if (*res == 'E') {
  476.     fprintf(stderr, "cannot end xact\n");
  477.     return (1);
  478.     }
  479.  
  480.     pfree(buf);
  481.     return (0);
  482. }
  483.  
  484. int
  485. readone()
  486. {
  487.     int fd;
  488.     int i, j;
  489.     int nbytes;
  490.     int nwritten;
  491.     char *res;
  492.     char *buf;
  493.     int *varlen;
  494.     int size;
  495.     int off;
  496.  
  497.     size = 8092;
  498.     if ((buf = (char *) palloc(size + 4)) == (char *) NULL) {
  499.     fprintf(stderr, "cannot palloc buf\n");
  500.     return (1);
  501.     }
  502.  
  503.     sprintf(QryBuf, "begin");
  504.     res = PQexec(QryBuf);
  505.  
  506.     if (*res == 'E') {
  507.     fprintf(stderr, "cannot begin xact\n");
  508.     return (1);
  509.     }
  510.  
  511.     if ((fd = p_open(FILENAME, INV_READ)) < 0) {
  512.     fprintf(stderr, "cannot open file\n");
  513.     return (1);
  514.     }
  515.  
  516.     myResetUsage();
  517.  
  518.     off = random() % ((TWENTYFIVE - 1) * MEGABYTE);
  519.     if (p_lseek(fd, off, L_SET) < 0) {
  520.     fprintf(stderr, "cannot lseek\n");
  521.     return (1);
  522.     }
  523.  
  524.     if ((nbytes = p_read(fd, buf, 1)) != 1) {
  525.     fprintf(stderr, "expected %d, got %d\n", 1, nbytes);
  526.     fflush(stderr);
  527.     return (1);
  528.     }
  529.  
  530.     myShowUsage();
  531.  
  532.     sprintf(QryBuf, "end");
  533.     res = PQexec(QryBuf);
  534.  
  535.     if (*res == 'E') {
  536.     fprintf(stderr, "cannot end xact\n");
  537.     return (1);
  538.     }
  539.  
  540.     pfree(buf);
  541.     return (0);
  542. }
  543.  
  544. int
  545. writeone()
  546. {
  547.     int fd;
  548.     int i, j;
  549.     int nbytes;
  550.     int nwritten;
  551.     char *res;
  552.     char *buf;
  553.     int *varlen;
  554.     int size;
  555.     int off;
  556.  
  557.     size = 8092;
  558.     if ((buf = (char *) palloc(size + 4)) == (char *) NULL) {
  559.     fprintf(stderr, "cannot palloc buf\n");
  560.     return (1);
  561.     }
  562.     varlen = (int *) buf;
  563.     *varlen = 5;
  564.  
  565.     sprintf(QryBuf, "begin");
  566.     res = PQexec(QryBuf);
  567.  
  568.     if (*res == 'E') {
  569.     fprintf(stderr, "cannot begin xact\n");
  570.     return (1);
  571.     }
  572.  
  573.     if ((fd = p_open(FILENAME, INV_READ)) < 0) {
  574.     fprintf(stderr, "cannot open file\n");
  575.     return (1);
  576.     }
  577.  
  578.     myResetUsage();
  579.  
  580.     off = random() % ((TWENTYFIVE - 1) * MEGABYTE);
  581.     if (p_lseek(fd, off, L_SET) < 0) {
  582.     fprintf(stderr, "cannot lseek\n");
  583.     return (1);
  584.     }
  585.  
  586.     if ((nbytes = p_write(fd, buf, 5)) != 1) {
  587.     fprintf(stderr, "expected %d, got %d\n", 1, nbytes);
  588.     fflush(stderr);
  589.     return (1);
  590.     }
  591.  
  592.     myShowUsage();
  593.  
  594.     sprintf(QryBuf, "end");
  595.     res = PQexec(QryBuf);
  596.  
  597.     if (*res == 'E') {
  598.     fprintf(stderr, "cannot end xact\n");
  599.     return (1);
  600.     }
  601.  
  602.     pfree(buf);
  603.     return (0);
  604. }
  605.  
  606. struct rusage Save_r;
  607. struct timeval Save_t;
  608.  
  609. myResetUsage()
  610. {
  611.     struct timezone tz;
  612.     getrusage(RUSAGE_SELF, &Save_r);
  613.     gettimeofday(&Save_t, &tz);
  614. }
  615.  
  616. myShowUsage()
  617. {
  618.     struct rusage r;
  619.     struct timeval user, sys;
  620.     struct timeval elapse_t;
  621.     struct timezone tz;
  622.  
  623.     getrusage(RUSAGE_SELF, &r);
  624.     gettimeofday(&elapse_t, &tz);
  625.     bcopy(&r.ru_utime, &user, sizeof(user));
  626.     bcopy(&r.ru_stime, &sys, sizeof(sys));
  627.     if (elapse_t.tv_usec < Save_t.tv_usec) {
  628.     elapse_t.tv_sec--;
  629.     elapse_t.tv_usec += 1000000;
  630.     }
  631.     if (r.ru_utime.tv_usec < Save_r.ru_utime.tv_usec) {
  632.     r.ru_utime.tv_sec--;
  633.     r.ru_utime.tv_usec += 1000000;
  634.     }
  635.     if (r.ru_stime.tv_usec < Save_r.ru_stime.tv_usec) {
  636.     r.ru_stime.tv_sec--;
  637.     r.ru_stime.tv_usec += 1000000;
  638.     }
  639.  
  640.     /* print stats in the format grokked by jbstats.awk */
  641.     printf(
  642.     "!\t%ld.%06ld %ld.%06ld %ld.%06ld\n",
  643.     elapse_t.tv_sec - Save_t.tv_sec,
  644.     elapse_t.tv_usec - Save_t.tv_usec,
  645.     r.ru_utime.tv_sec - Save_r.ru_utime.tv_sec,
  646.     r.ru_utime.tv_usec - Save_r.ru_utime.tv_usec,
  647.     r.ru_stime.tv_sec - Save_r.ru_stime.tv_sec,
  648.     r.ru_stime.tv_usec - Save_r.ru_stime.tv_usec);
  649.     fflush(stdout);
  650. }
  651.